﻿using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfToolkit
{

    /// <summary>
    /// UCWebView.xaml 的交互逻辑
    /// </summary>
    public partial class UCWebView : UserControl
    {
        public UCWebView()
        {
            InitializeComponent();
            this.btnLeft.IsEnabled = false;
            this.btnRight.IsEnabled = false;
            this.Loaded += UCWebView_Loaded;
        }

        private void UCWebView_Loaded(object sender, RoutedEventArgs e)
        {

        }

        #region 依赖属性

        public string Path
        {
            get { return (string)GetValue(PathProperty); }
            set { SetValue(PathProperty, value); }
        }


        public static readonly DependencyProperty PathProperty = DependencyProperty.Register("Path", typeof(string), typeof(UCWebView), new PropertyMetadata("", UrlReflash));


        #endregion



        #region 方法

        /// <summary>
        /// Url刷新加载
        /// </summary>
        public static void UrlReflash(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UCWebView control = d as UCWebView;
            try
            {
                string nv = (string)e.NewValue;
                control.txUrl.Text = nv;
                control.wb.Source = new Uri(nv);

            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        /// <summary>
        /// 点击前往
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Path = this.txUrl.Text;
            this.wb.Source = new Uri(Path);
            this.wb.EnsureCoreWebView2Async().Wait();
            GetLeftRightShow();
        }

        /// <summary>
        /// 检测回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnGo.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }
        }

        private void GetLeftRightShow()
        {
            //后退
            if (this.wb.CanGoBack)
            {
                this.btnLeft.IsEnabled = true;
            }
            else
            {
                this.btnLeft.IsEnabled = false;
            }
            //前进
            if (this.wb.CanGoForward)
            {
                this.btnRight.IsEnabled = true;
            }
            else
            {
                this.btnRight.IsEnabled = false;
            }
        }

        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            if (this.wb.CanGoBack)
            {
                this.wb.GoBack();
            }
            GetLeftRightShow();
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            if (this.wb.CanGoForward)
            {
                this.wb.GoForward();
            }
            GetLeftRightShow();
        }
    }
 
}
